﻿using Common;
using MuseLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.DataVisualization.Charting;

namespace MuseUI.ObservableData
{
    public class AccelerometerObservableData : IObservableData
    {
        public AccelerometerObservableData()
        {
            _accelerometerData = new ScrollingList<int>[SERIES_COUNT];
            for (int i = 0; i < SERIES_COUNT; i++)
            {
                _accelerometerData[i] = new ScrollingList<int>(POINT_COUNT, 0);
            }

            _allSeries.Add(new Series("Accelerometer1") { Color = Colours.BlueForWhiteBackground, ChartType = SeriesChartType.Line, ChartArea = "Scroll" });
            _allSeries.Add(new Series("Accelerometer2") { Color = Colours.GreenForWhiteBackground, ChartType = SeriesChartType.Line, ChartArea = "Scroll" });
            _allSeries.Add(new Series("Accelerometer3") { Color = Colours.RedForWhiteBackground, ChartType = SeriesChartType.Line, ChartArea = "Scroll" });

            _chartAreas.Add(ChartingHelper.InitialiseChartArea("Scroll", new Double[] { 0, 10, 0, 1024 }, new float[] { 1, 1, 50, 98 }, true));
        }

        private object _lockInsertData = new object();
        public void InsertData(DateTime date, int[] data)
        {
            var clonedSeries = new List<Series>();
            for (int i = 0; i < SERIES_COUNT; i++)
            {
                lock (_lockInsertData)
                    _accelerometerData[i].AddItem(data[i]);
            }

            _currentTimeOffset += TIME_DELTA;
        }

        public void OutputRecordedData(String filePath)
        {
            throw new NotImplementedException();
        }

        public List<Series> UpdateSeries()
        {
            var clonedSeries = new List<Series>();

            for (int i = 0; i < SERIES_COUNT; i++)
            {
                clonedSeries.Add(_allSeries[i].Clone());
            }

            for (int pointIndex = 0; pointIndex < POINT_COUNT; pointIndex++)
            {
                for (int seriesIndex = 0; seriesIndex < SERIES_COUNT; seriesIndex++)
                {
                    clonedSeries[seriesIndex].Points.AddXY(10 - ((Double)pointIndex * TIME_DELTA), _accelerometerData[seriesIndex][pointIndex]);
                }
            }

            return clonedSeries;
        }

        private List<Series> _allSeries = new List<Series>();
        public List<Series> AllSeries
        {
            get { return _allSeries; }
        }

        private List<ChartArea> _chartAreas = new List<ChartArea>();
        public List<ChartArea> ChartAreas
        {
            get { return _chartAreas; }
        }

        private bool _isVisible;
        public bool IsVisible
        {
            get { return _isVisible; }
            set { _isVisible = value; }
        }

        private BindingList<ObservableDataOption> _chartingOptions = new BindingList<ObservableDataOption>();
        public BindingList<ObservableDataOption> ChartingOptions
        {
            get { return _chartingOptions; }
        }

        private bool _isRecording;

        public bool IsRecording
        {
            get { return _isRecording; }
            set { _isRecording = value; }
        }

        public MuseChartType ChartType { get { return MuseChartType.Accelerometer; } }

        private ScrollingList<int>[] _accelerometerData;
        private Double _currentTimeOffset;
        private const Double TIME_DELTA = 1.0 / 50.0;
        private const int SERIES_COUNT = 3;
        private const int POINT_COUNT = 50 * 10;
    }
}
